/*
 * @Author: duanguang
 * @Date: 2019-11-07 09:31:57
 * @LastEditTime: 2021-10-27 22:26:26
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-server/app/aop/loginAop.ts
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import ResponseModel from '../model/responseModel';
import { UserInfoEntity } from '../model/user';

interface ILoginEntity extends UserInfoEntity {
  lastLoginTime: string;
}
/**
 * 登陆成功后做最后登录时间的记录
 */
export default function loginAop(_target, _name: string, descriptor: PropertyDescriptor) {
  const oldValue = descriptor.value;
  descriptor.value = async function () {
    const result = await oldValue.apply(this, arguments) as ResponseModel<ILoginEntity>;
    if (result.success && result.data) {
      const userInfo = result.data;
      // @ts-ignore
      await this.ctx.model.User.updateOne({
        uuid: userInfo.uuid,
      }, {
        lastLoginTime: userInfo.lastLoginTime,
      });
    }
    return result;
  };
  return descriptor;
}
